package c.main.core;

import c.main.jdbc.JdbcTemplate;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * Author: chengwei
 * Date:   2016/3/15
 * Time:   14:10
 */
public class WcsContext {
    
    private JdbcTemplate sysJdbcTemplate;

    public JdbcTemplate getSysJdbcTemplate() {
        return sysJdbcTemplate;
    }

    public void setSysJdbcTemplate(JdbcTemplate sysJdbcTemplate) {
        this.sysJdbcTemplate = sysJdbcTemplate;
    }

    private Map<String,DataSource> dataSourceMap = new HashMap<>();
    
    private Map<String,Object> beanMap = new HashMap<>();

    public Map<String, Object> getBeanMap() {
        return beanMap;
    }
    
    public <T> T getBean(String s, Class<T> clazz) {
        Object o = beanMap.get(s);
        if (o == null) {
            throw new RuntimeException(String.format("Bean[%s]没有找到..",s));
        }
        return (T)o;
    }
    
    public <T> T getBean(Class<T> clazz) {
        return getBean(clazz.getName(),clazz);
    }
    
    public void addBean (Object o) {
        beanMap.put(o.getClass().getName(),o);
    }

    public void addBean (String key,Object o) {
        beanMap.put(key,o);
    }

    public DataSource getDataSourceMap(String s) {
        return dataSourceMap.get(s);
    }

    //系统默认的数据源
    public DataSource getDataSourceMap() {
        return dataSourceMap.get("ds");
    }

    public void setDataSource(String s,DataSource dataSource) {
        dataSourceMap.put(s,dataSource);
    }
}
